﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace park.ModelCode
{
    public partial class ModelRulesAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request.Params["m"] != null)
                {
                    Response.ContentType = "application/json";
                    string m = Request.Params["m"].ToLower();
                    string r = "{}";
                    switch (m)
                    {
                        case "search":

                            r = GetInfo();
                            break;
                    }
                    Response.Write(r);
                    Response.End();
                }
            }
        }

       public string fix(string key)
        {
            if (Request.Params[key] != null)
            {
                try
                {
                    return Uri.UnescapeDataString(Request.Params[key]);
                }
                catch
                {
                    return Request.Params[key];
                }
            }
            return "";
        }


        string GetInfo()
        {
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            #region 正式代码
            //string strID = fix("id");
            //cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

            //string json = sl.GetModelRules(strID);

            //return json;
            #endregion

            #region 测试代码
            string resultModelType = ""
                + "[{"
                + "\"ID\": \"MB\","
                + "\"ModeName\": \"咪表\","
                + "\"AddUser\": \"张三\","
                + "\"AddDate\": \"2012-8-20 22:22:30\""
                + "}]";
            string resultRules = ""
                + "[{"
                + "\"Name\": \"类型\","
                + "\"Length\": 2,"
                + "\"Description\": \"属性说明\","
                + "\"Format\": \"自定义格式\""
                + "},"
                + "{"
                + "\"Name\": \"类型\","
                + "\"Length\": 2,"
                + "\"Description\": \"属性说明\","
                + "\"Format\": \"自定义格式\""
                + "}]";
            string json = string.Empty;

            json = "{\"ModelType\":" + resultModelType + ",\"Rules\":" + resultRules + "}";

            return json;
            //测试代码---------------------------
            #endregion
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            string id = Request.Form["ID"];

            string[] aryName = Request.Form.GetValues("txtName");
            string[] aryLength = Request.Form.GetValues("txtLength");
            string[] aryDescription = Request.Form.GetValues("txtDescription");
            string[] aryFormat = Request.Form.GetValues("txtFormat");
            List<int> listLength = new List<int>();
                        
            foreach(string sb in aryLength)
            {
                int i = 0;
                if(int.TryParse(sb,out i ))
                {
                    listLength.Add(i);
                }
            }
            int[] intLength = listLength.ToArray();

            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            bool flag = sl.UpdModelRules(id, "test", DateTime.Now.ToShortDateString(), aryName, intLength, aryDescription, aryFormat);

            string msg = flag ? "数据保存成功！" : "数据保存失败，请重试...";

            Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('" + msg + "');</script>");
        }
    }
}